package com.wlc.rabbitmq.topicqueue;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 王立朝
 * @description 主题消费者
 * @date 2021-12-15 10:09:39
 */
@Component
public class TopicConsumer2 {

    @RabbitListener(queues = "topic_queue_2")
    public void readMessage(Message message, Channel channel) throws IOException {
       try{
           int a = 2/0;
           channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

           System.out.println("我是消费信息222：" + new String(message.getBody()));
       }catch (Exception e){
           System.out.println("发生了异常");
           // 如果发生异常，最后的参数： false: 把消息抛弃  true： 把消息放回到队列里面
           channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
           //channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
       }

    }

}
